package com.lewis.module.m2.api.bill.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.core.entity.controller.BaseController;
import com.lewis.common.core.utils.StringUtil;
import com.lewis.module.m2.api.bill.code.entity.BillCode;
import com.lewis.module.m2.api.bill.code.service.IBillCodeService;
import com.lewis.module.m2.api.bill.entity.Bill;
import com.lewis.module.m2.api.bill.service.IBillService;
import com.lewis.module.m2.api.bill.vo.BillDeleteVo;
import com.lewis.module.m2.api.sysUserQq.entity.SysUserQq;
import com.lewis.module.m2.api.sysUserQq.service.ISysUserQqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * billController
 *
 * @author lewis
 * @since 2021-11-02
 */
@Slf4j
@Api(tags = "账单")
@RestController
@RequestMapping("/api/bill")
public class BillApi extends BaseController {

	@Resource
	private IBillService billService;

	@Resource
	private IBillCodeService billCodeService;

	@Resource
	private ISysUserQqService sysUserQqService;

	@ApiOperation(value = "查询bill列表", notes = "")
	@GetMapping("/list")
	public BaseResult list(Bill bill) {
		List<String> msgList = new ArrayList<>();
		List<Bill> billList = extracted(bill, msgList);
		if (billList == null) {
			return BaseResult.fail().setMsg("查无此人");
		}
		for (int i = 0; i < billList.size(); i++) {
			// 只收纳100个数据
			if (i == 100) {
				break;
			}
			Bill bill1 = billList.get(i);
			String type = bill1.getBillType() == 1 ? "收入" : "支出";
			// id 类型 金额 备注 时间
			msgList.add(bill1.getId() + " " + type + " " + bill1.getMoney() + " " + bill1.getRemark() + " " + bill1.getCtime() + "\n");
		}
		return BaseResult.ok().setData(msgList);
	}

	@ApiOperation(value = "查询余额", notes = "")
	@GetMapping("/balance")
	public BaseResult balance(Bill bill) {
		List<String> msgList = new ArrayList<>();
		extracted(bill, msgList);
		return BaseResult.ok().setData(msgList);
	}

	private List<Bill> extracted(Bill bill, List<String> msgList) {
		SysUserQq one = sysUserQqService.getOne(
				new LambdaQueryWrapper<SysUserQq>()
						.eq(SysUserQq::getQq, bill.getQq())
		);
		if (one == null) {
			return null;
		}
		List<Bill> billList = billService.list(
				new LambdaQueryWrapper<Bill>()
						.eq(StringUtil.isNotNull(bill.getBillType()), Bill::getBillType, bill.getBillType())
						.eq(Bill::getUserId, one.getUserId())
						.eq(StringUtil.isNotNull(bill.getId()), Bill::getId, bill.getId())
						.eq(StringUtil.isNotNull(bill.getBillCodeId()), Bill::getBillCodeId, bill.getBillCodeId())
						.le(StringUtil.isNotNull(bill.getCtime()), Bill::getCtime, bill.getCtime())
						.orderByDesc(Bill::getId)
		);

		BigDecimal incomeAll = new BigDecimal(0);
		BigDecimal payAll = new BigDecimal(0);

		for (Bill bill1 : billList) {
			if (bill1.getBillType() == 1) {
				incomeAll = incomeAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
			} else {
				payAll = payAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
			}
		}

		msgList.add("总收入：" + incomeAll + "\n");
		msgList.add("总支出：" + payAll + "\n");
		msgList.add("总余额：" + incomeAll.subtract(payAll));

		return billList;
	}

	@ApiOperation(value = "新增bill", notes = "")
	@PostMapping("/add")
	public BaseResult add(@RequestBody Bill bill) {
		// 确保该用户有该账单
		BillCode one = billCodeService.getById(bill.getBillCodeId());
		if (one == null) {
			return BaseResult.fail().setMsg("名下无此账单");
		}
		List<String> msgList = new ArrayList<>();
		SysUserQq one1 = sysUserQqService.getOne(
				new LambdaQueryWrapper<SysUserQq>()
						.eq(SysUserQq::getQq, bill.getQq())
		);
		if (one1 == null) {
			return BaseResult.fail().setMsg("查无此人");
		}
		bill.setUserId(one1.getUserId());
		if (!billService.save(bill)) {
			return BaseResult.fail().setMsg("记账失败");
		}
		msgList.add("记账完毕，要保持记账的好习惯哦~\n");
		// 账单余额
		List<Bill> billList = billService.list(
				new LambdaQueryWrapper<Bill>()
						.eq(Bill::getUserId, bill.getUserId())
						.eq(StringUtil.isNotNull(bill.getBillCodeId()), Bill::getBillCodeId, bill.getBillCodeId())
		);
		BigDecimal incomeAll = new BigDecimal(0);
		BigDecimal payAll = new BigDecimal(0);

		for (Bill bill1 : billList) {
			if (bill1.getBillType() == 1) {
				incomeAll = incomeAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
			} else {
				payAll = payAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
			}
		}

		msgList.add("[ " + one.getBillName() + " ] 总余额：" + incomeAll.subtract(payAll));

		return BaseResult.ok().setData(msgList);
	}

	@ApiOperation(value = "删除明细", notes = "")
	@PostMapping("/delete")
	public BaseResult delete(@RequestBody BillDeleteVo billDeleteVo) {
		List<String> msgList = new ArrayList<>();
		String qq = billDeleteVo.getQq();
		Long id = billDeleteVo.getId();
		SysUserQq one1 = sysUserQqService.getOne(
				new LambdaQueryWrapper<SysUserQq>()
						.eq(SysUserQq::getQq, qq)
		);
		if (one1 == null) {
			return BaseResult.fail().setMsg("查无此人");
		}
		LambdaQueryWrapper<Bill> billLambdaQueryWrapper = new LambdaQueryWrapper<>();
		billLambdaQueryWrapper.eq(Bill::getUserId, one1.getUserId());
		billLambdaQueryWrapper.eq(Bill::getId, id);
		Bill bill = billService.getOne(billLambdaQueryWrapper);
		if (bill == null) {
			return BaseResult.fail().setMsg("禁止删除其他人的明细");
		}
		if (billService.remove(billLambdaQueryWrapper)) {
			msgList.add("删除明细完毕，要保持记账的好习惯哦~\n");

			// 账单余额
			List<Bill> billList = billService.list(
					new LambdaQueryWrapper<Bill>()
							.eq(Bill::getUserId, one1.getUserId())
							.eq(Bill::getBillCodeId, billDeleteVo.getBillCodeId())
			);
			BigDecimal incomeAll = new BigDecimal(0);
			BigDecimal payAll = new BigDecimal(0);

			for (Bill bill1 : billList) {
				if (bill1.getBillType() == 1) {
					incomeAll = incomeAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
				} else {
					payAll = payAll.add(new BigDecimal(Double.toString(bill1.getMoney())));
				}
			}
			BillCode one = billCodeService.getById(bill.getBillCodeId());
			msgList.add("【" + one.getBillName() + "】总余额：" + incomeAll.subtract(payAll));
			return BaseResult.ok().setData(msgList);
		}
		return BaseResult.fail().setMsg("删除失败");
	}
}